package mxm.amfire;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;

import me.yokeyword.fragmentation.SupportFragment;
import mxm.amfire.event.StartFragEvent;

/**
 * Created by hm on 2017/2/13.
 */

public class FuncFragment extends SupportFragment implements View.OnClickListener{
    private View rootView;
    private RecyclerView mRecyclerView;
    private PopupWindow popupWindow;

    public FuncFragment() {
    }

    public static FuncFragment newInstance() {
        FuncFragment fragment = new FuncFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_func, container, false);
        initView();
        return rootView;
    }

    private void initView() {
        mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
        mRecyclerView.setLayoutManager(new GridLayoutManager(_mActivity,2));
        mRecyclerView.setAdapter(new RecyclerView.Adapter<MyViewHolder>() {
            @Override
            public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                MyViewHolder myViewHolder = new MyViewHolder(LayoutInflater.from(_mActivity).inflate(R.layout.recycer_item_func, null, false));
                return myViewHolder;
            }

            @Override
            public void onBindViewHolder(MyViewHolder holder, int position) {
                //holder.iv.setImageResource(res[position]);
                //holder.tv.setText(titles[position]);
                holder.itemView.setOnClickListener(FuncFragment.this);
            }

            @Override
            public int getItemCount() {
                return 4;
            }


        });
    }
    /**
     * 创建PopupWindow
     */
    protected void initPopuptWindow() {
        // TODO Auto-generated method stub
        // 获取自定义布局文件activity_popupwindow_left.xml的视图
        View popupWindow_view = _mActivity.getLayoutInflater().inflate(R.layout.layout_window_func, null,
                false);
        // 创建PopupWindow实例,200,LayoutParams.MATCH_PARENT分别是宽度和高度
        popupWindow = new PopupWindow(popupWindow_view, 200, LinearLayout.LayoutParams.MATCH_PARENT, true);
        // 设置动画效果
        //popupWindow.setAnimationStyle(R.style.AnimationFade);
        // 点击其他地方消失
        popupWindow_view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                    popupWindow = null;
                }
                return false;
            }
        });
    }

    /***
     * 获取PopupWindow实例
     */
    private void getPopupWindow() {
        if (null != popupWindow) {
            popupWindow.dismiss();
            return;
        } else {
            initPopuptWindow();
        }
    }

    @Override
    public void onClick(View view) {
        startActivity(new Intent(_mActivity,ZhiYunActivity.class));
        //EventBus.getDefault().post(new StartFragEvent(ZhiYunFragment.newInstance()));
        //getPopupWindow();
        // 这里是位置显示方式,在屏幕的左侧
       // popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
    }

    class MyViewHolder extends RecyclerView.ViewHolder
    {
        ImageView iv;
        TextView tv;
        public MyViewHolder(View view)
        {
            super(view);
            iv= (ImageView) view.findViewById(R.id.imageView);
            tv= (TextView) view.findViewById(R.id.textView);
        }
    }
}
